package util;

import org.apache.commons.dbcp2.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils {
    public static DataSource dataSource = null;

    static {
        Properties properties = new Properties();//新建配置文件对象
        InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            dataSource = BasicDataSourceFactory.createDataSource(properties);//创建数据源对象
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    Connection getConnection() throws SQLException {
        return dataSource.getConnection();//获取数据库连接对象
    }

}
